import { legacy_createStore, applyMiddleware } from "redux";
import thunk from "redux-thunk";
import logger from "redux-logger";
import "../api/data"
import { ALLACTION, ALLSTATE, GETLIST, SAVEINDEX, GETIMGS, GETCLASSIFY, GETSHOPCAR } from "../types/store.d"
const initialState = {
    imgsArr: [],
    listArr: [],
    classifyArr: [],
    shopcarArr: [],
    rightArr: []
    // JSON.parse(window.localStorage.getItem('imgsArr') as string) || 
}

const reducer = (state: ALLSTATE = initialState, { type, payload }: ALLACTION) => {
    switch (type) {
        case GETIMGS:
            let newArr = [...state.imgsArr]
            newArr = payload;
            state.imgsArr = newArr;
            window.localStorage.setItem('imgsArr', JSON.stringify(state.imgsArr))
            return { ...state }
        case GETLIST:
            let Arr = [...state.listArr]
            Arr = payload;
            state.listArr = Arr;
            window.localStorage.setItem('listArr', JSON.stringify(state.listArr))
            return { ...state }
        case GETCLASSIFY:
            let ARR = [...state.classifyArr]
            ARR = payload;
            state.classifyArr = ARR;
            state.rightArr =ARR[0].info as any
            window.localStorage.setItem('classifyArr', JSON.stringify(state.classifyArr))
            return { ...state }
        case GETSHOPCAR:
            let arr = [...state.shopcarArr]
            arr.push(payload);
            state.shopcarArr = arr;
            window.localStorage.setItem('shopcarArr', JSON.stringify(state.shopcarArr))
            return { ...state }
        case SAVEINDEX:
            let arr1 = [...state.classifyArr]
            state.rightArr =arr1[payload].info as any
            window.localStorage.setItem('rightArr', JSON.stringify(state.rightArr))
            return { ...state }
        default:
            return state
    }
}


const store = legacy_createStore(reducer, applyMiddleware(thunk, logger))
export default store;